package game.manager;

import game.Constants;
import game.Event;
import game.Person;
import game.util.PersonUtil;

import java.util.Collection;
import java.util.Date;
import java.util.Observable;
import java.util.Vector;

public class PersonManager implements Manager {
	private Person user;
	private Collection<Person> people;

	@Override
	public void gameUpdate() {

	}

	private static PersonManager manager;

	public static PersonManager getManager() {
		if (manager == null)
			manager = new PersonManager();
		return manager;
	}

	private PersonManager() {
		this.initialize();
	}
	
	public Person getUser() {
		return user;
	}

	public void addPerson(Person person) {
		if (!this.people.contains(person))
			this.people.add(person);
	}

	public void addPeople(Collection<Person> newPeople) {
		for (Person person : newPeople) {
			this.addPerson(person);
		}
	}

	@Override
	public void update(Observable o, Object arg) {
		final Event event = (Event) arg;

		switch (event.getType()) {
		case RESET:
			this.initialize();
			break;
		case GAME_STARTED:
			this.user.setEnergy(Constants.ENERGY_MAX);
			this.generateRandomPeople();
			break;
		case MINUTE_PASSED:
			final Date currentDate = new Date();
			for (Person person : this.people) {
				person.expireModifiers(currentDate);
			}
			break;
		case HOUR_PASSED:
			this.generateRandomPeople();
		}
	}

	private void generateRandomPeople() {
		final PersonUtil generator = new PersonUtil();
		this.addPeople(generator
				.generateRandomPeople(Constants.NUM_NEW_PEOPLE_PER_HOUR));
	}

	@Override
	public void initialize() {
		this.people = new Vector<Person>();
		this.user = new Person();
	}

	public Collection<Person> getPeople() {
		return new Vector<Person>(this.people);
	}
}
